//
//  ShareManager.m
//  bestkeep
//
//  Created by 武探 on 2016/11/29.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "ShareManager.h"
#import <UShareUI/UShareUI.h>
#import "ShareMenuView.h"

@implementation ShareObject

@end



@interface ShareManager()

@end

@implementation ShareManager

-(instancetype)initPrivate {
    self = [super init];
    if(self) {
        [self configShare];
    }
    return self;
}

-(void)configShare {
    
    UMSocialManager *umManager = [UMSocialManager defaultManager];
    //打开调试日志
    [umManager openLog:!ApplicationEnvironmentProduction];
    //设置友盟appkey
    [umManager setUmSocialAppkey:@"53fbf399fd98c5a48f01c81f"];
    
    [umManager setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx2c7367321c4ecab5" appSecret:@"b41ce1f1f4f10c33396c61575b80e61d" redirectURL:nil];
    [umManager setPlaform:UMSocialPlatformType_QQ appKey:@"1104979766" appSecret:@"7XLhdBUX0QdU53SR" redirectURL:nil];
    
    UMSocialShareUIConfig *uiConfig = [UMSocialShareUIConfig shareInstance];
    uiConfig.shareTitleViewConfig.isShow = NO;
    
}

+(instancetype)manager {
    static ShareManager *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[ShareManager alloc] initPrivate];
    });
    return instance;
}

/**
-(void)showShareMenuInController:(__weak UIViewController*)controller sharePlatformSelectionBlock:(ShareObject*(^)(SharePlatformType type))block callBack:(CompeletionCallBack)callback{
    
    NSArray *platforms = [[UMSocialManager defaultManager] platformTypeArray];
    if(!platforms || platforms.count == 0) {
        [HUD showErrorMessage:@"没有可以分享的平台"];
        return;
    }
    
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        if(!block) {
            return;
        }
        
        ShareObject *obj = block((SharePlatformType)platformType);
        
        if(!obj) {
            return;
        }
        
        UMSocialMessageObject *umMessageObj = [UMSocialMessageObject messageObject];
        
        UMShareWebpageObject *shareObj = [[UMShareWebpageObject alloc] init];
        shareObj.title = obj.title;
        shareObj.descr = obj.descr;
        shareObj.thumbImage = obj.thumbImage;
        shareObj.webpageUrl = obj.webpageUrl;
        
        umMessageObj.shareObject = shareObj;
        
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:umMessageObj currentViewController:controller completion:^(id result, NSError *error) {
            
            if(callback) {
                callback(result, error);
            }
        }];
    }];
}
*/

-(void)showShareMenuInController:(__weak UIViewController*)controller sharePlatformSelectionBlock:(ShareObject*(^)(SharePlatformType type))block callBack:(CompeletionCallBack)callback {
    NSArray *platforms = [[UMSocialManager defaultManager] platformTypeArray];
    if(!platforms || platforms.count == 0) {
        [HUD showErrorMessage:@"没有可以分享的平台"];
        return;
    }
    
    ShareMenuView *menuView = [[ShareMenuView alloc] init];
    menuView.items = platforms;
    UIView *backView = [[UIApplication sharedApplication] keyWindow];
    [menuView showWithView:backView platformSelectionBlock:^(SharePlatformType type) {
        if(!block) {
            return;
        }
        
        ShareObject *obj = block(type);
        
        if(!obj) {
            return;
        }
        
        UMSocialMessageObject *umMessageObj = [UMSocialMessageObject messageObject];
        
        UMShareWebpageObject *shareObj = [[UMShareWebpageObject alloc] init];
        shareObj.title = obj.title;
        shareObj.descr = obj.descr;
        shareObj.thumbImage = obj.thumbImage;
        shareObj.webpageUrl = obj.webpageUrl;
        
        umMessageObj.shareObject = shareObj;
        
        [[UMSocialManager defaultManager] shareToPlatform:(UMSocialPlatformType)type messageObject:umMessageObj currentViewController:controller completion:^(id result, NSError *error) {
            
            if(callback) {
                callback(result, error);
            }
        }];
    }];
}

@end
